<template>
  <div class="box">
    <!-- 订单管理 -->

    <div class="head">
      <el-row>
        <el-col :span="4">
          <el-input
            v-model="nickname"
            clearable
            placeholder="请输入用户呢称"
          ></el-input>
        </el-col>
        <el-col :span="4">
          <el-input
            v-model="consignee"
            clearable
            placeholder="请输入收货人名称"
          ></el-input>
        </el-col>
        <el-col :span="4">
          <el-input
            v-model="orderSn"
            clearable
            placeholder="请输入订单编号"
          ></el-input>
        </el-col>
        <el-col :span="6">
          <el-date-picker
            v-model="value2"
            type="datetimerange"
            :picker-options="pickerOptions"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            align="right"
          >
          </el-date-picker>
        </el-col>
        <el-col :span="4">
          <el-select v-model="value1" multiple placeholder="请选择订单状态">
            <el-option
              v-for="item in options"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </el-col>
      </el-row>
    </div>
    <div class="banner">
      <el-row>
        <el-col :span="6">
          <el-button type="primary" icon="el-icon-search" @click="lookUp"
            >查找</el-button
          >
          <el-button
            type="primary"
            icon="el-icon-bottom"
            @click="handleDownload"
            >导出</el-button
          >
        </el-col>
      </el-row>
    </div>
    <div class="body">
      <el-table :data="tableData" style="width: 100%" border>
        <el-table-column type="expand">
          <template v-slot="scope">
            <div
              class="body-box"
              v-for="(item, index) in scope.row.goodsVoList"
              :key="index"
            >
              <img :src="item.picUrl" alt="" />
              <span>商品名称：{{ item.goodsName }}</span>
              <span
                >规格：
                <span
                  v-for="(items, indexs) in item.specifications"
                  :key="indexs"
                  >{{ items }}
                </span></span
              >
              <div>单价：{{ item.price }}</div>
              <div>数量：{{ item.number }}</div>
              <div>小计：{{ item.number * item.price }}</div>
            </div>
          </template>
        </el-table-column>
        <el-table-column label="订单编号" prop="orderSn"> </el-table-column>
        <el-table-column label="用户头像" prop="userAvatar">
          <template v-slot="scope">
            <img
              v-if="scope.row.userAvatar"
              :src="scope.row.userAvatar"
              alt=""
            />
            <div v-else class="imgBox"></div>
          </template>
        </el-table-column>
        <el-table-column label="下单用户" prop="userName"> </el-table-column>
        <el-table-column label="下单时间" prop="addTime"> </el-table-column>
        <el-table-column label="订单状态" prop="orderStatus">
          <template v-slot="scope">
            <el-button
              type="primary"
              size="mini"
              plain
              v-if="scope.row.orderStatus == 301"
              >已发货</el-button
            >
            <el-button
              type="primary"
              size="mini"
              plain
              v-if="scope.row.orderStatus == 103"
              >系统取消</el-button
            >
            <el-button
              type="primary"
              size="mini"
              plain
              v-if="scope.row.orderStatus == 101"
              >未付款</el-button
            >
            <el-button
              type="primary"
              size="mini"
              plain
              v-if="scope.row.orderStatus == 102"
              >用户取消</el-button
            >
            <el-button
              type="primary"
              size="mini"
              plain
              v-if="scope.row.orderStatus == 201"
              >已付款</el-button
            >
            <el-button
              type="primary"
              size="mini"
              plain
              v-if="scope.row.orderStatus == 202"
              >申请退款</el-button
            >
            <el-button
              type="primary"
              size="mini"
              plain
              v-if="scope.row.orderStatus == 203"
              >已退款</el-button
            >
            <el-button
              type="primary"
              size="mini"
              plain
              v-if="scope.row.orderStatus == 401"
              >用户收货</el-button
            >
            <el-button
              type="primary"
              size="mini"
              plain
              v-if="scope.row.orderStatus == 402"
              >系统收货</el-button
            >
          </template>
        </el-table-column>
        <el-table-column label="订单金额" prop="orderPrice"> </el-table-column>
        <el-table-column label="实付金额" prop="actualPrice"> </el-table-column>
        <el-table-column label="支付时间" prop="payTime"> </el-table-column>
        <el-table-column label="收货人" prop="consignee">
          <template v-slot="scope">
            <p style="color: red; font-weight: 600">
              {{ scope.row.consignee }}
            </p>
          </template>
        </el-table-column>
        <el-table-column label="收货电话" prop="mobile"> </el-table-column>
        <el-table-column label="物流单号" prop="shipSn"> </el-table-column>
        <el-table-column label="物流渠道" prop="shipChannel"> </el-table-column>
        <el-table-column label="操作" width="380">
          <template v-slot="scope">
            <el-button type="primary" size="mini" @click="details(scope.row.id)"
              >详情</el-button
            >
            <el-button type="danger" size="mini" @click="open1(scope.row.id)"
              >删除</el-button
            >
            <el-button type="warning" size="mini" @click="collection(scope.row)"
              >收款</el-button
            >
            <el-button type="primary" size="mini" @click="Deliver(scope.row)"
              >发货</el-button
            >
            <el-button type="danger" size="mini" @click="refund(scope.row)"
              >退款</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="body-box">
      <el-dialog
        :title="title"
        :visible.sync="dialogVisible"
        :width="widthData"
        :before-close="handleClose"
        :center="title == '订单收款'"
      >
        <span>
          <div v-if="title == '订单详情'">
            <div ref="print1">
              <div class="body-boxCon">
                <div>
                  <span
                    style="
                      font-weight: 600;
                      font-size: 18px;
                      display: inline-block;
                      width: 80px;
                    "
                    >订单编号
                  </span>
                  {{ orderData.orderSn }}
                </div>
                <div>
                  <span
                    style="
                      font-weight: 600;
                      font-size: 18px;
                      display: inline-block;
                      width: 80px;
                    "
                    >订单状态
                  </span>
                  <el-tag size="small" v-if="orderData.orderStatus == 103"
                    >系统取消</el-tag
                  >
                  <el-tag size="small" v-if="orderData.orderStatus == 101"
                    >未付款</el-tag
                  >
                  <el-tag size="small" v-if="orderData.orderStatus == 102"
                    >用户取消</el-tag
                  >
                  <el-tag size="small" v-if="orderData.orderStatus == 201"
                    >已付款</el-tag
                  >
                  <el-tag size="small" v-if="orderData.orderStatus == 202"
                    >申请退款</el-tag
                  >
                  <el-tag size="small" v-if="orderData.orderStatus == 203"
                    >已退款</el-tag
                  >
                  <el-tag size="small" v-if="orderData.orderStatus == 301"
                    >已发货</el-tag
                  >
                  <el-tag size="small" v-if="orderData.orderStatus == 401"
                    >用户收货</el-tag
                  >
                  <el-tag size="small" v-if="orderData.orderStatus == 402"
                    >系统收货</el-tag
                  >
                </div>
                <div>
                  <span
                    style="
                      font-weight: 600;
                      font-size: 18px;
                      display: inline-block;
                      width: 80px;
                    "
                    >订单用户
                  </span>
                  {{ userData.nickname }}
                </div>
                <div>
                  <span
                    style="
                      font-weight: 600;
                      font-size: 18px;
                      display: inline-block;
                      width: 80px;
                    "
                    >用户留言
                  </span>
                  {{ orderData.message }}
                </div>
                <div>
                  <span
                    style="
                      font-weight: 600;
                      font-size: 18px;
                      display: inline-block;
                      width: 80px;
                    "
                    >收货信息 </span
                  >(收货人) {{ orderData.consignee }}(手机号)
                  {{ orderData.mobile }}(地址) {{ orderData.address }}
                </div>
                <div>
                  <span
                    style="
                      font-weight: 600;
                      font-size: 18px;
                      display: inline-block;
                      width: 80px;
                    "
                    >商品信息
                  </span>
                  <div style="margin-top: 10px" class="table">
                    <el-table :data="orderTable" border style="width: 100%">
                      <el-table-column
                        prop="goodsName"
                        label="商品名称"
                        width="180"
                      >
                      </el-table-column>
                      <el-table-column
                        prop="goodsId"
                        label="商品编号"
                        width="180"
                      >
                      </el-table-column>
                      <el-table-column label="货品规格">
                        <template v-slot="scope">
                          <span
                            v-for="(item, index) in scope.row.specifications"
                            :key="index"
                            >{{ item }}</span
                          >
                        </template>
                      </el-table-column>
                      <el-table-column prop="price" label="货品价格">
                      </el-table-column>
                      <el-table-column prop="number" label="货品数量">
                      </el-table-column>
                      <el-table-column label="货品图片">
                        <template v-slot="scope">
                          <img :src="scope.row.picUrl" alt="" />
                        </template>
                      </el-table-column>
                    </el-table>
                  </div>
                </div>
                <div>
                  <span
                    style="
                      font-weight: 600;
                      font-size: 18px;
                      display: inline-block;
                      width: 80px;
                    "
                    >费用信息
                  </span>
                  (实际费用) {{ orderData.actualPrice }}元 = (商品总价){{
                    orderData.orderPrice
                  }}
                  元 + (快递费用){{ orderData.freightPrice }}元 - (优惠减免){{
                    orderData.couponPrice
                  }}
                  元 - (积分减免){{ orderData.integralPrice }}元
                </div>
                <div>
                  <span
                    style="
                      font-weight: 600;
                      font-size: 18px;
                      display: inline-block;
                      width: 80px;
                    "
                    >支付信息
                  </span>
                  (支付信息)微信支付(支付时间){{ orderData.payTime }}
                </div>
                <div>
                  <span
                    style="
                      font-weight: 600;
                      font-size: 18px;
                      display: inline-block;
                      width: 80px;
                    "
                    >快递信息
                  </span>
                  (快递公司){{ orderData.shipChannel }} (快递单号)
                  {{ orderData.shipSn }} (发货时间) {{ orderData.shipTime }}
                </div>
                <div>
                  <span
                    style="
                      font-weight: 600;
                      font-size: 18px;
                      display: inline-block;
                      width: 80px;
                    "
                    >退款信息
                  </span>
                  (退款金额) 元(退款类型) (退款备注) (退款时间)
                </div>
                <div>
                  <span
                    style="
                      font-weight: 600;
                      font-size: 18px;
                      display: inline-block;
                      width: 80px;
                    "
                    >收货信息
                  </span>
                  (确认收货时间)
                </div>
              </div>
            </div>
          </div>
          <div v-else-if="title == '退款'" class="body-boxCon">
            <div>
              <el-row>
                <el-col :span="3">
                  <span
                    style="
                      font-weight: 600;
                      font-size: 18px;
                      display: inline-block;
                      width: 80px;
                    "
                    >退款金额
                  </span>
                </el-col>
                <el-col :span="20">
                  <el-input
                    placeholder="请输入内容"
                    v-model="refundData"
                    :disabled="true"
                  >
                  </el-input>
                </el-col>
              </el-row>
            </div>
          </div>
          <div v-else-if="title == '订单收款'" class="body-boxCon">
            <div>
              确定当前订单 (订单编号 {{ collectionData.orderSn }})
              已经完成线下收款？
            </div>
            <div>
              <el-row>
                <el-col :span="4">
                  <span
                    style="
                      font-weight: 600;
                      font-size: 18px;
                      display: inline-block;
                      width: 80px;
                    "
                    >订单金额
                  </span>
                </el-col>
                <el-col :span="6">
                  <el-input
                    v-model="collectionData.actualPrice"
                    :disabled="true"
                  >
                  </el-input>
                </el-col>
              </el-row>
            </div>
            <div>
              <el-row>
                <el-col :span="4">
                  <span
                    style="
                      font-weight: 600;
                      font-size: 18px;
                      display: inline-block;
                      width: 80px;
                    "
                    >付款金额
                  </span>
                </el-col>
                <el-col :span="6">
                  <el-input v-model="collectionData.actualPrice"> </el-input>
                </el-col>
              </el-row>
            </div>
            <div>
              <el-table :data="collectionData.goodsVoList" style="width: 100%">
                <el-table-column prop="goodsName" label="商品" width="180">
                </el-table-column>
                <el-table-column label="规格" width="180">
                  <template v-slot="scope">
                    <span
                      v-for="(item, index) in scope.row.specifications"
                      :key="index"
                      >{{ item }}</span
                    >
                  </template>
                </el-table-column>
                <el-table-column prop="number" label="下单数量">
                </el-table-column>
              </el-table>
            </div>
          </div>
          <div v-else-if="title == '发货'" class="body-boxCon">
            <div>
              <el-row>
                <el-col :span="3">
                  <span
                    style="
                      font-weight: 600;
                      font-size: 18px;
                      display: inline-block;
                      width: 80px;
                    "
                    >快递公司
                  </span>
                </el-col>
                <el-col :span="6">
                  <el-select
                    placeholder="请选择"
                    v-model="Express"
                    size="medium"
                  >
                    <el-option
                      v-for="(item, index) in DeliverData"
                      :key="index"
                      :label="item.value"
                      :value="item.value"
                    >
                    </el-option>
                  </el-select>
                </el-col>
              </el-row>
            </div>
            <div>
              <el-row>
                <el-col :span="3">
                  <span
                    style="
                      font-weight: 600;
                      font-size: 18px;
                      display: inline-block;
                      width: 80px;
                    "
                    >快递公司
                  </span>
                </el-col>
                <el-col :span="14">
                  <el-input
                    v-model="ExpressCorporation"
                    size="medium"
                    placeholder="请输入内容"
                  ></el-input>
                </el-col>
              </el-row>
            </div>
          </div>
        </span>
        <span slot="footer" class="dialog-footer">
          <el-button @click="dialogVisible = false">取 消</el-button>
          <el-button
            v-if="title == '订单详情'"
            type="primary"
            @click="handleDy"
          
            >打印</el-button
          >
          <el-button
            v-else-if="title == '退款'"
            type="primary"
            @click="refundClick"
            >确定</el-button
          >

          <el-button
            v-else-if="title == '订单收款'"
            type="primary"
            @click="collectionClick"
            >确定</el-button
          >
          <el-button
            v-else-if="title == '发货'"
            type="primary"
            @click="DeliverClick"
            >确定</el-button
          >
        </span>
      </el-dialog>
    </div>

    <div class="foot">
      <paging
        :total="total"
        :page="pageData.page"
        @size="Eachpage"
        @page="currentPage"
      ></paging>
    </div>
  </div>
</template>

<script>
import {
  listOrder,
  detailOrder,
  deleteOrder,
  refundOrder,
  payOrder,
  shipOrder,
} from "../../api/order";

import paging from "../../utils/paging.vue";
export default {
 
  components: {
    paging,
  },
  data() {
    return {
      input: "",
      title: "",
      refundData: "",
      refundId: "",
      Express: "",
      ExpressCorporation: "",
      ExpressName: "",
      dialogVisible: false,
      DeliverData: [
        {
          id: 1319,
          value: "中通快递",
        },
        {
          id: 1319,
          value: "圆通快递",
        },
        {
          id: 1319,
          value: "韵达快递",
        },
        {
          id: 1319,
          value: "邮政快递包裹",
        },
        {
          id: 1319,
          value: "EMS",
        },
        {
          id: 1319,
          value: "德邦快递",
        },
        {
          id: 1319,
          value: "快捷快递",
        },
        {
          id: 1319,
          value: "宅急送",
        },
        {
          id: 1319,
          value: "TNT快递",
        },
        {
          id: 1319,
          value: "UPS",
        },
        {
          id: 1319,
          value: "DHL",
        },
        {
          id: 1319,
          value: "FEDEX联邦(国内件)",
        },
        {
          id: 1319,
          value: "FEDEX联邦(国际件)",
        },
      ],
      options: [
        {
          value: "101",
          label: "未付款",
        },
        {
          value: "102",
          label: "用户取消",
        },
        {
          value: "103",
          label: "系统取消",
        },
        {
          value: "201",
          label: "已付款",
        },
        {
          value: "202",
          label: "申请退款",
        },
        {
          value: "203",
          label: "已退款",
        },
        {
          value: "301",
          label: "已发货",
        },
        {
          value: "401",
          label: "用户收货",
        },
        {
          value: "402",
          label: "系统收货",
        },
      ],
      nickname: "",
      consignee: "",
      orderSn: "",
      total: 20,
      valueSelect: "",
      pickerOptions: {
        shortcuts: [
          {
            text: "最近一周",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
              picker.$emit("pick", [start, end]);
            },
          },
          {
            text: "最近一个月",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
              picker.$emit("pick", [start, end]);
            },
          },
          {
            text: "最近三个月",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
              picker.$emit("pick", [start, end]);
            },
          },
        ],
      },
      value2: "",
      value1: "",
      tableData: [],
      tableDatas: [],
      tableDataed: [],
      pageData: {
        page: 1,
        limit: 20,
        sort: "add_time",
        order: "desc",
        start: null,
        end: null,
      },
      downloadLoading: false,
      detailsData: [],
      orderData: [],
      userData: [],
      orderTable: [],
      collectionData: [],
      widthData: "60%",
    };
  },

  mounted() {},
  created() {
    this.listTable();
  },

  methods: {
    // 打印确定
    handleDy(){
      this.$print(this.$refs.print1)
    },
    // 发货确定
    DeliverClick() {
      this.dialogVisible = false;
      // console.log(this.Express , this.ExpressCorporation);
      let id = this.DeliverData.filter((res) => {
        return res.value == this.Express;
      });
      // console.log(id);

      shipOrder({
        orderId: id[0].id,
        shipChannel: this.ExpressName,
        shipSn: this.ExpressCorporation,
      }).then((res) => {
        // console.log(res);
        this.$notify({
          title: "失败",
          message: "当前订单状态不支持发货",
          type: "error",
        });
      });
    },
    // 发货
    Deliver(data) {
      this.title = "发货";
      this.dialogVisible = true;
      this.widthData = "50%";
      this.Express = "";
      this.ExpressCorporation = "";
      // console.log(data.userName);
      this.ExpressName = data.userName;
    },
    // 收款确定
    collectionClick(e) {
      this.dialogVisible = false;
      payOrder({
        goodsList: this.collectionData.goodsVoList,
        newMoney: this.collectionData.actualPrice,
        oldMoney: this.collectionData.actualPrice,
        orderId: this.collectionData.id,
        orderSn: this.collectionData.orderSn,
      }).then((res) => {
        // console.log(res);
      });
      this.$notify({
        title: "失败",
        message: "当前订单状态不支持线下收款",
        type: "error",
      });
    },
    // 收款
    collection(e) {
      this.title = "订单收款";
      this.dialogVisible = true;
      this.collectionData = e;
      this.widthData = "40%";
      console.log(e, "首款确定");
    },
    // 退款
    refund(e) {
      this.title = "退款";
      this.widthData = "50%";
      this.dialogVisible = true;
      this.refundData = e.actualPrice;
      this.refundId = e.id;
    },
    // 删除
    open1(id) {
      deleteOrder({ orderId: id }).then((res) => {
        // console.log(res)
        if (res.data.errmsg == "成功") {
          this.$notify({
            title: "成功",
            message: "订单删除成功",
            type: "success",
          });
        } else {
          this.$notify({
            title: "失败",
            message: "该订单不能删除",
            type: "error",
          });
        }
      });
    },
    // 弹出确定
    refundClick() {
      this.dialogVisible = false;

      refundOrder({
        orderId: this.refundId,
        refundMoney: this.refundData,
      }).then((res) => {});
      this.$notify({
        title: "失败",
        message: "该订单不能确定收货",
        type: "error",
      });
    },
    // 弹出
    handleClose(done) {
      this.$confirm("确认关闭？")
        .then((_) => {
          done();
        })
        .catch((_) => {});
    },
    // 点击详情
    details(e) {
      // console.log(id);
      this.title = "订单详情";
      this.widthData = "60%";
      this.dialogVisible = true;
      detailOrder(e).then((res) => {
        console.log(res);
        this.orderData = res.data.data.order;
        this.userData = res.data.data.user;
        this.orderTable = res.data.data.orderGoods;
      });
    },
    // 导出
    handleDownload() {
      this.downloadLoading = true;
      import("@/vendor/Export2Excel").then((excel) => {
        const tHeader = ["用户ID", "商品ID", "添加时间"];
        const filterVal = ["userId", "valueId", "addTime"];
        excel.export_json_to_excel2(
          tHeader,
          this.list,
          filterVal,
          "用户收藏信息"
        );
        this.downloadLoading = false;
      });
    },
    // 点击查找
    lookUp() {
      console.log(this.value2[0].getFullYear());
      var startH = this.value2[0].getFullYear();
      if (this.value2[0].getMonth() + 1 > 10) {
        var startM = this.value2[0].getMonth() + 1;
      } else {
        var startM = Number("0" + this.value2[0].getMonth() + 1);
      }
      if (this.value2[1].getMonth() + 1 > 10) {
        var endM = this.value2[1].getMonth() + 1;
      } else {
        var endM = "0" + (this.value2[1].getMonth() + 1);
      }

      var startD = this.value2[0].getDate();
      var endH = this.value2[1].getFullYear();
      var endD = this.value2[1].getDate();
      var start = startH + "-" + startM + "-" + startD + " " + "00:00:00";
      var end = endH + "-" + endM + "-" + endD + " " + "00:00:00";
      listOrder({
        page: 1,
        limit: 20,
        nickname: this.nickname,
        consignee: this.consignee,
        timeArray: start,
        timeArray: end,
        orderStatusArray: "",
        sort: "add_time",
        order: "desc",
        start: start,
        end: end,
      }).then((res) => {
        // console.log(res);
        this.tableData = res.data.data.list;
        this.total = res.data.data.total;
      });
    },
    // 渲染页面
    listTable() {
      listOrder(this.pageData).then((res) => {
        // console.log(res);
        this.tableData = res.data.data.list;
        this.total = res.data.data.total;
      });
    },
    // 每页几条
    Eachpage(val) {
      // console.log(val);
      this.pageData.page = 1;
      this.pageData.limit = val;
      this.listData();
    },
    // 当前页数
    currentPage(val) {
      // console.log(val);
      this.pageData.page = val;
      // console.log(this.pageData.page);

      this.listTable();
    },
  },
};
</script>

<style lang="scss" scoped>
.table img {
  width: 40px;
  height: 40px;
}
.body-boxCon > div {
  margin: 30px 0;
  font-size: 18px;
}
.body-box {
  display: flex;
  flex-direction: row;
  justify-content: space-around;
  align-items: center;
  padding: 20px 0;
}
.imgBox {
  width: 40px;
  height: 40px;
  background-color: #ccc;
  border-radius: 50%;
  margin: 0 auto;
}
::v-deep .el-table th {
  text-align: center !important;
}
::v-deep .el-table tr td {
  text-align: center !important;
}
.body {
  margin-top: 20px;
}
.body img {
  width: 40px;
  height: 40px;
  border-radius: 50%;
}
.box {
  padding: 20px;
}
.banner {
  margin-top: 20px;
}
.demo-table-expand {
  font-size: 0;
}
.demo-table-expand label {
  width: 90px;
  color: #99a9bf;
}
.demo-table-expand .el-form-item {
  margin-right: 0;
  margin-bottom: 0;
  width: 50%;
}
</style>